package com.vue.utils.quartz;

import org.quartz.*;
import org.quartz.core.jmx.CronTriggerSupport;
import org.quartz.impl.StdSchedulerFactory;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Calendar;

/**
 * Created by Administrator on 2018/7/3.
 */
public class QuartzManager {

    public static void main(String[] args) {
        try {
            long time = new Date().getTime();
            time = time + 7000;
            Date date = new Date(time);

            //scheduler
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

            //定义一个Trigger
            /*Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1")//name/group
                    .startNow()
                    .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                            .withIntervalInSeconds(1)//每隔一秒执行一次
                            .repeatForever())//永远执行
                    .build();*/

            //.withIntervalInHours(1) //每小时执行一次
            //.withIntervalInMinutes(1) //每分钟执行一次
            //.withRepeatCount(10) //次数为10次

            /*SimpleTrigger trigger = (SimpleTrigger)TriggerBuilder.newTrigger()
                    .withIdentity("trigger1", "group1")
                    .startAt(date).build();//设置执行时间*/


            Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1")//name/group
                    .startNow()
                    .withSchedule(DailyTimeIntervalScheduleBuilder.dailyTimeIntervalSchedule()
                            .startingDailyAt(TimeOfDay.hourAndMinuteOfDay(9,0))//每天9:00开始
                            .endingDailyAt(TimeOfDay.hourAndMinuteOfDay(16,0))//每天16点结束
                            .onDaysOfTheWeek(Calendar.MONDAY,Calendar.TUESDAY,Calendar.WEDNESDAY,Calendar.THURSDAY,Calendar.FRIDAY)
                            .withIntervalInHours(1)//间隔一小时
                            .withRepeatCount(100)//最懂重复100次,实际执行101次
                    ).build();


            //定义一个jobdetail
            JobDetail jobDetail = JobBuilder.newJob(HelloQuartz.class)
                    .withIdentity("job1", "group1")
                    .usingJobData("name", "quartz")
                    .build();

            //加入这个调度
            scheduler.scheduleJob(jobDetail,trigger);

            //启动
            scheduler.start();

            Thread.sleep(3*60*60*1000);
            scheduler.shutdown();


        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
